package pt.utl.ist.ginger3d.xml;

import java.util.Stack;

import nu.xom.Element;
import nu.xom.NodeFactory;
import nu.xom.Nodes;
import nu.xom.Attribute.Type;

public class GfsNodeFactory extends NodeFactory {

	Stack<Element> structStack, typeStack, templateStack;
	Stack<Element> currentStack;
	
	public GfsNodeFactory() {
		structStack = new Stack<Element>();
		typeStack = new Stack<Element>();
		templateStack = new Stack<Element>();
	}
	
	@Override
	public Element startMakingElement(String name, String namespace) {
		if(name.equals("structure")) {
			currentStack = structStack;
		}
		else if(name.equals("types")) {
			currentStack = typeStack;
		}
		else if(name.equals("templates")) {
			currentStack = templateStack;
		}
		Element e = super.startMakingElement(name, namespace);
		if(currentStack != null) {
			currentStack.push(e);
		}
		return e;
	}
	
	@Override
	public Nodes makeAttribute(String name, String URI, String value, Type type) {
		System.out.println(currentStack.peek().getLocalName() + "/@" + name + "=" + value);
		return super.makeAttribute(name, URI, value, type);
	}
	
	@Override
	public Nodes finishMakingElement(Element element) {
		
		if(currentStack != null) {
			currentStack.pop();
			if(element.getLocalName().equals("structure") || 
			   element.getLocalName().equals("types") || 
			   element.getLocalName().equals("templates")) {
				
				currentStack = null;
			}
		}
		return super.finishMakingElement(element);
	}
}
